<template>
  <div class="container-fluid">
    <div class=" container pans">
      <div class="panel panel-success">
        <div class="panel-heading">
          <p class="panel-title">
            {{title}}
          </p>
        </div>
        <div class="panel-body">
          <div>
            <el-input placeholder="请输入管理员账号" v-model="name">
              <template slot="prepend">账号</template>
            </el-input>
          </div>
          <br/>
          <div>
            <el-input placeholder="请输入密码" v-model="pwd_visible" type="password">
              <template slot="prepend">密码</template>
            </el-input>
          </div>
          <el-button :plain="true" v-on:click="submit" class='button' type="success">提交</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
var md5 = require('md5');
var logInfo={
  title:"",
  type:-1,
  name:"",
  pwd:"",
  pwd_visible:"",
  expire:24
}
var title=""
var path=window.location.pathname
logInfo.title="管理员登录"
export default {
  name: 'login',
  data () {
    return logInfo
  },
  methods:{
    submit:function () {
      var vm=this
      this.pwd=md5(this.pwd_visible)
      this.pwd_visible=""
      var url='/api/adminLogin'
      fetch(url,{
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      mode: 'cors',
      body: JSON.stringify({
        name:logInfo.name,
        pwd:logInfo.pwd,
        expire:logInfo.expire
      })
    }).then(function(response) {
      logger.log(response)
      if (response.status >= 200) {
        return response.json()
      } else {
        var error = new Error(response.statusText)
        error.response = response
        throw error
      }
    }).then(function(json) {
      if(json.code==0){
        setCookie("admins",JSON.stringify(json.data),168)
        vm.$message({
          message:'登录成功!',
          type:'success',
          onClose:function () {
            vm.$router.push('/admin')
          }
        })

      }else{
        vm.$message({
          message:'登录失败！',
          type:'error'
        })
      }
    })
    }
  }
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
